package com.ruoyi.web.controller.hypermarket.webPage;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.system.domain.ProCommidity;
import com.ruoyi.system.service.IProCommidityService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;


/**
 * 商城管理类
 */
@Controller
@RequestMapping("/hypermarket/webPage/")
public class WebPageController extends BaseController {

    private final String hypermarket_privateKey = "c235es1owhdoakhdfa53869a";

    @Autowired
    IProCommidityService proCommidityService;

    private String prefix = "hypermarket/webPage/";

    @RequiresPermissions("hypermarket:webPage:view")
    @GetMapping(value = "list")
    public String toList()
    {
        return prefix + "list";
    }


    @RequiresPermissions("hypermarket:webPage:add")
    @GetMapping(value = "addCommodity")
    public String addCommodity(ModelMap modelMap) throws Exception {
        //拿到当前登录角色信息
        SysUser user = getSysUser();
        if(StringUtils.isNotNull(user)){
            modelMap.put("userName",user.getUserName());
            modelMap.put("userId", StringUtil.encodeBy3DES(hypermarket_privateKey,user.getUserId().toString(),"UTF-8"));
        }
        return prefix + "addCommodity";
    }






    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【上传商品】
     */
    @RequiresPermissions("system:commidity:add")
    @Log(title = "【上传商品】", businessType = BusinessType.INSERT)
    @PostMapping("/doAddCommodity")
    @ResponseBody
    public AjaxResult addSave(ProCommidity proCommidity,ModelMap modelMap) throws Exception {
        //拿到当前登录角色信息
        SysUser user = getSysUser();
        if(StringUtils.isNotNull(user)){
            modelMap.put("userName",user.getUserName());
            modelMap.put("userId", StringUtil.encodeBy3DES(hypermarket_privateKey,user.getUserId().toString(),"UTF-8"));
        }
        Date time = new Date();
        proCommidity.setCrateTime(time);
        proCommidity.setUptateTime(time);
        proCommidity.setState("E");
        proCommidity.setOperator(user.getUserName());
        return toAjax(proCommidityService.insertProCommidity(proCommidity));
    }

}
